package com.yaorange.teaching.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yaorange.common.core.utils.DateUtils;
import com.yaorange.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.yaorange.teaching.mapper.YaCourseDesignMapper;
import com.yaorange.teaching.domain.YaCourseDesign;
import com.yaorange.teaching.service.IYaCourseDesignService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 课程设计Service业务层处理
 *
 * @author ruoyi
 * @date 2024-11-16
 */
@Service
public class YaCourseDesignServiceImpl extends ServiceImpl<YaCourseDesignMapper,YaCourseDesign> implements IYaCourseDesignService
{
    @Autowired
    private YaCourseDesignMapper yaCourseDesignMapper;

    /**
     * 查询课程设计
     *
     * @param id 课程设计主键
     * @return 课程设计
     */
    @Override
    public YaCourseDesign selectYaCourseDesignById(Long id)
    {
        return yaCourseDesignMapper.selectYaCourseDesignById(id);
    }

    /**
     * 查询课程设计列表
     *
     * @param yaCourseDesign 课程设计
     * @return 课程设计
     */
    @Override
    public List<YaCourseDesign> selectYaCourseDesignList(YaCourseDesign yaCourseDesign)
    {
        return yaCourseDesignMapper.selectYaCourseDesignList(yaCourseDesign);
    }

    /**
     * 新增课程设计
     *
     * @param yaCourseDesign 课程设计
     * @return 结果
     */
    @Override
    public int insertYaCourseDesign(YaCourseDesign yaCourseDesign)
    {
        yaCourseDesign.setCreateTime(DateUtils.getNowDate());
        return yaCourseDesignMapper.insertYaCourseDesign(yaCourseDesign);
    }

    /**
     * 修改课程设计
     *
     * @param yaCourseDesign 课程设计
     * @return 结果
     */
    @Override
    public int updateYaCourseDesign(YaCourseDesign yaCourseDesign)
    {
        yaCourseDesign.setUpdateTime(DateUtils.getNowDate());
        return yaCourseDesignMapper.updateYaCourseDesign(yaCourseDesign);
    }

    /**
     * 批量删除课程设计
     *
     * @param ids 需要删除的课程设计主键
     * @return 结果
     */
    @Override
    public int deleteYaCourseDesignByIds(Long[] ids)
    {
        return yaCourseDesignMapper.deleteYaCourseDesignByIds(ids);
    }

    /**
     * 删除课程设计信息
     *
     * @param id 课程设计主键
     * @return 结果
     */
    @Override
    public int deleteYaCourseDesignById(Long id)
    {
        return yaCourseDesignMapper.deleteYaCourseDesignById(id);
    }

    @Override
    @Transactional(readOnly = true)
    public List<YaCourseDesign> selectYaCourseDesignListByStageId(Long stageId) {
        return yaCourseDesignMapper.selectYaCourseDesignListByStageId(stageId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateYaCourseDesign(Integer stage, List<YaCourseDesign> courseDesignList) {
        /*
        courseDesignList.forEach(item->{
            item.setCreateTime(nowDate);
            item.setCreateBy(SecurityUtils.getUsername());
        });
        saveBatch(courseDesignList,100);*/

        //考虑到三种 情况，1.新增，2.修改，3.删除
        //1.先把需要删除的数据删除(避免删除掉新增的)
        List<Long> idsList = courseDesignList.stream().filter(item->ObjectUtils.isNotEmpty(item.getId())).map(YaCourseDesign::getId).collect(Collectors.toList());
        yaCourseDesignMapper.deleteYaCourseDesignByIdsNotIn(idsList);
        //2.新增或者修改
        List<YaCourseDesign> updateList = new ArrayList<>(courseDesignList.size());
        List<YaCourseDesign> addList = new ArrayList<>(courseDesignList.size());
        Date nowDate = DateUtils.getNowDate();
        String username = SecurityUtils.getUsername();
        courseDesignList.forEach(cd->{
            //判断是否有id
            if (ObjectUtils.isEmpty(cd.getId())) {
                cd.setCreateTime(nowDate);
                cd.setCreateBy(username);
                addList.add(cd);
            } else {
                cd.setUpdateTime(nowDate);
                cd.setUpdateBy(username);
                updateList.add(cd);
            }
        });
        saveBatch(addList,100);
        updateBatchById(updateList,100);
        return 1;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int editRelatedInterview(Integer cdId, List<Integer> interviewIdList) {
        //1.删除该课程的所有关联面试题
        yaCourseDesignMapper.deleteRelatedInterview(cdId);
        //2.批量新增中间表
        interviewIdList.forEach(interviewId->{
            yaCourseDesignMapper.insertCourseDesignInterview(cdId,interviewId);
        });
        return 1;
    }
}
